package com.ruoyi.common;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Utils {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        List<Map<String, Object>> timeRanges = getRecentHalfYearTimeRanges(currentDate);
        for (Map<String, Object> timeRange : timeRanges) {
            System.out.println(timeRange);
        }
    }

    public static List<Map<String, Object>> getRecentHalfYearTimeRanges(LocalDate currentDate) {
        List<Map<String, Object>> timeRanges = new ArrayList<>();
        LocalDate startDate = currentDate.minusMonths(6).withDayOfMonth(26);
        LocalDate endDate = currentDate.plusDays(1);

        while (startDate.isBefore(endDate)) {
            String startFormatted = startDate.withDayOfMonth(26).minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            String endFormatted = startDate.plusMonths(1).withDayOfMonth(25).minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            String formattedDate = startDate.format(DateTimeFormatter.ofPattern("yy-MM"));

            Map<String, Object> timeRange = new HashMap<>();
            timeRange.put("beginDate", startFormatted);
            timeRange.put("endDate", endFormatted);
            timeRange.put("date", formattedDate);
            timeRanges.add(timeRange);

            startDate = startDate.plusMonths(1);
        }

        return timeRanges;
    }
}

